<!--
 * @Descripttion: 入库表单
 * @LastEditors: xzh
 * @LastEditTime: 2023-10-13 22:26:30
-->
<template>
  <DileDrawer
    v-model="inBoundForm.visible"
    :loading="inBoundForm.loading"
    size="70%"
    @sure="hanldeSure"
  >
    <template #header>
      <span>{{
        $t(`table.btnArr.${!inBoundForm.formData.id ? "add" : "edit"}`)
      }}</span>
    </template>
    <template #default>
      <el-form
        ref="formRef"
        :model="inBoundForm.formData"
        :rules="formRules"
        label-width="6rem"
        size="large"
        status-icon
      >
        <el-col :span="12">
          <el-form-item :label="$t('page.equipSpareArr.inBoundManageArr.code')">
            <el-input
              v-model="inBoundForm.formData.code"
              disabled
              :placeholder="$t('common.systemAutoGenerate')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            :label="$t('page.equipSpareArr.inBoundManageArr.name')"
            prop="name"
          >
            <el-input
              v-model="inBoundForm.formData.name"
              :placeholder="$t('common.enter')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            :label="$t('page.equipSpareArr.inBoundManageArr.type')"
            prop="typeName"
          >
            <el-select
              v-model="inBoundForm.formData.typeName"
              :placeholder="$t('common.select')"
            >
              <el-option
                v-for="inBoundType in Object.keys(inBoundTypeEnum)"
                :key="inBoundType"
                :label="
                  $t(
                    `page.equipSpareArr.inBoundManageArr.typeArr.${inBoundType}`
                  )
                "
                :value="inBoundType"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('page.equipSpareArr.spareLedgerArr.code')">
            <el-input
              v-model="inBoundForm.formData.relSpare.code"
              disabled
              :placeholder="$t('common.systemAutoBringOut')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            :label="$t('page.equipSpareArr.spareLedgerArr.name')"
            prop="sparePartId"
            :validate-status="
              !!inBoundForm.formData.sparePartId ? 'success' : ''
            "
          >
            <!-- 输入框选择对象 -->
            <DileInputSelectItem
              v-model="inBoundForm.formData.sparePartId"
              :expand-on-click-node="false"
              :selectType="selectTypeEnum.equipSpare"
              :cacheLabel="inBoundForm.formData.relSpare.name"
              @itemSelected="handleSpareSelected"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            :label="$t('page.equipSpareArr.spareLedgerArr.specification')"
          >
            <el-input
              v-model="inBoundForm.formData.relSpare.specification"
              disabled
              :placeholder="$t('common.systemAutoBringOut')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('page.equipSpareArr.spareType')">
            <el-input
              v-model="inBoundForm.formData.relSpare.extJson.typeName"
              disabled
              :placeholder="$t('common.systemAutoBringOut')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('page.equipSpareArr.spareLedgerArr.unit')">
            <el-input
              v-model="inBoundForm.formData.relSpare.unit"
              disabled
              :placeholder="$t('common.systemAutoBringOut')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            :label="$t('page.equipSpareArr.spareLedgerArr.inventoryUpperLimit')"
          >
            <el-input
              v-model="inBoundForm.formData.relSpare.inventoryUpperLimit"
              disabled
              :placeholder="$t('common.systemAutoBringOut')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            :label="$t('page.equipSpareArr.spareLedgerArr.inventoryLowerLimit')"
          >
            <el-input
              v-model="inBoundForm.formData.relSpare.inventoryLowerLimit"
              disabled
              :placeholder="$t('common.systemAutoBringOut')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            :label="$t('page.equipSpareArr.inBoundManageArr.currentInventory')"
          >
            <el-input
              v-model="inBoundForm.formData.relSpare.inventoryTotalLimit"
              disabled
              :placeholder="$t('common.systemAutoBringOut')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            :label="$t('page.equipSpareArr.inBoundManageArr.stockInNumber')"
            prop="stockInNumber"
          >
            <el-input-number
              v-model="inBoundForm.formData.stockInNumber"
              controls-position="right"
              :min="0"
              :placeholder="$t('common.enter')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            :label="$t('page.equipSpareArr.inBoundManageArr.positionCode')"
          >
            <el-input
              v-model="inBoundForm.formData.positionCode"
              :placeholder="$t('common.enter')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            :label="$t('page.equipSpareArr.inBoundManageArr.store')"
            prop="warehouseId"
            :validate-status="
              !!inBoundForm.formData.warehouseId ? 'success' : ''
            "
          >
            <!-- 输入框选择对象 -->
            <DileInputSelectItem
              v-model="inBoundForm.formData.warehouseId"
              :expand-on-click-node="false"
              :selectType="selectTypeEnum.store"
              :cacheLabel="inBoundForm.formData.extJson.warehouseName"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('common.remark')">
            <el-input
              v-model="inBoundForm.formData.remark"
              :rows="5"
              type="textarea"
            />
          </el-form-item>
        </el-col>
      </el-form>
    </template>
  </DileDrawer>
</template>

<script setup lang="ts">
import { ref, reactive, toRefs, computed, nextTick } from "vue";
import type { object_Inf } from "@/utils/typings/index.type";
import { useI18n } from "vue-i18n";
import { ElNotification } from "element-plus";
import { selectTypeEnum } from "@/components/dile-selectItem/index.type";
import { inBoundTypeEnum } from "@/utils/typings/equipManage/equipSpare/index.type";
import DileInputSelectItem from "@/components/dile-selectItem/selectItem-input/index.vue";
import DileDrawer from "@/components/dile-drawer/index.vue";
import Dile_APi from "@/api";

const emit = defineEmits(["refresh"]);

let { t } = useI18n();

/**
 * @Author: xzh
 * @Descripttion: 表单默认数据
 * @Param:
 */
let defaultData = () => ({
  id: 0,
  name: "", //入库名称
  code: "", //入库编码
  typeName: "", //入库类型
  sparePartId: null, //备件
  currentInventory: 0, //当前库存
  stockInNumber: 0, //入库数量
  positionCode: "", //存放位置编码
  warehouseId: null, //入库仓库
  remark: "", //备注
  extJson: {
    warehouseName: "", //仓库名称
  },
  //管理备件信息
  relSpare: {
    code: "", //备件编号
    name: "", //备件名称
    specification: "", //规格型号
    typeId: null, //备件类型
    unit: "", //单位
    inventoryUpperLimit: "", //库存上限
    inventoryLowerLimit: "", //库存下限
    inventoryTotalLimit: "", //总库存
    extJson: {
      typeName: "", //备件类型名称
    },
  },
});
/**
 * @Author: xzh
 * @Descripttion: 入库表单参数
 * @Param:
 */
let inBoundForm = reactive<object_Inf>({
  visible: false,
  loading: false,
  //表单数据
  formData: defaultData(),
});

/**
 * @Author: xzh
 * @Descripttion: 表单实例
 * @Param:
 */
let formRef = ref<any>(null);

/**
 * @Author: xzh
 * @Descripttion:表单验证规则
 * @Param:
 * @param {*} computed
 */
let formRules = computed((): any => {
  return reactive({
    name: [
      {
        required: true,
        message:
          t("common.enter") +
          t("page.organizeManageArr.departmentArr.departName"),
        trigger: "change",
      },
    ],
    typeName: [
      {
        required: true,
        message:
          t("common.enter") + t("page.equipSpareArr.inBoundManageArr.type"),
        trigger: "change",
      },
    ],
    sparePartId: [
      {
        required: true,
        message:
          t("common.enter") + t("page.equipSpareArr.spareLedgerArr.name"),
        trigger: "change",
      },
    ],
    stockInNumber: [
      {
        required: true,
        message:
          t("common.enter") +
          t("page.equipSpareArr.inBoundManageArr.stockInNumber"),
        trigger: "change",
      },
    ],
    positionCode: [
      {
        required: true,
        message:
          t("common.enter") +
          t("page.equipSpareArr.inBoundManageArr.positionCode"),
        trigger: "change",
      },
    ],
    warehouseId: [
      {
        required: true,
        message:
          t("common.select") + t("page.equipSpareArr.inBoundManageArr.store"),
        trigger: "change",
      },
    ],
  });
});

/**
 * @Author: xzh
 * @Descripttion:初始化加载
 * @Param:
 * @param {*} id 入库主键
 */
let init = async (id: number) => {
  let { visible, loading } = toRefs(inBoundForm);
  visible.value = true;
  await nextTick();
  try {
    loading.value = true;
    //加载表单数据
    await initFormData(id);
  } finally {
    loading.value = false;
  }
};

/**
 * @Author: xzh
 * @Descripttion: 加载表单数据
 * @Param:
 */
let initFormData = async (id: number) => {
  let { model, sparePartLedgerModel } = !!id
      ? await Dile_APi.getEquipInBoundInfo(id)
      : { model: defaultData(), sparePartLedgerModel: {} },
    { relSpare } = inBoundForm.formData;

  Object.assign(inBoundForm.formData, {
    ...model,
    relSpare: {
      ...relSpare,
      ...sparePartLedgerModel,
    },
  });
};

/**
 * @Author: xzh
 * @Descripttion: 备件选择
 * @Param:
 * @param {*} dataArr
 */
let handleSpareSelected = (dataArr: Array<object_Inf>) => {
  let spare = dataArr.find(() => true);
  Object.assign(inBoundForm.formData.relSpare, spare);
};

/**
 * @Author: xzh
 * @Descripttion: 保存事件
 * @Param:
 */
let hanldeSure = async () => {
  let { formData, loading, visible } = toRefs(inBoundForm),
    valid = await formRef.value.validate();
  if (!valid || !!loading.value) {
    return false;
  }
  try {
    loading.value = true;
    await Dile_APi.saveEquipInBound(formData.value);
    //触发刷新回调
    emit("refresh");
    //提示成功
    ElNotification({
      message: t("common.message.OperationSuccess"),
      type: "success",
    });
    //关闭弹框
    visible.value = false;
  } finally {
    loading.value = false;
  }
};

defineExpose({
  init,
});
</script>
<style lang="scss" scoped></style>
